<?php

class Core_Component_Menu_Model_Menu extends Soul_Model_Entity_Version {

    protected $tableName = '{t}menu_information';

    public function getCategory() {
        if (!$this->issetData('category')) {
            if ($this->issetData('category_id')) {
                $this->data['category'] = App::loadModel('menu/category', $this->getCategoryId());
            }
        }
        return $this->data['category'];
    }
    
    public function getQuery(){
        return parent::getQuery()
                    ->field('c.name', 'category_name')
                    ->joinInner('{t}menu_category', 'c', 'm.category_id', 'c.id');
    }
    
    protected function beforeInsert() {
        $this->setWebsiteId(App::getRunningWebsite()->getId());
        if(!$this->issetData('parent_id')){
           $this->setParentId(0);
        }
        if(!$this->issetData('sort')){
            $sort = $this->getQuery()
                        ->select('MAX(m.sort)')
                        ->condition('m.parent_id', $this->getParentId())
                        ->fetch()
                        ->getFirstValue();
            $this->setSort($sort + 1);
        }        
        parent::beforeInsert();
    }

    protected function afterInsert() {
    	$this->markNoChanged();
    	$idPath = $this->getId().'_';
    	if($this->issetData('parent_id') && $this->getParentId() != 0){
    		$parent = new self();
    		$idPath = $parent->load($this->getParentId())->getIdPath().$idPath;
    	}
    	$sql = 'UPDATE `'.$this->getTableName().'` SET'
    		 . ' `id_path`=:idPath:s'
    		 . ' WHERE `id` =:id:i';
    	$this->getDb()->setSql($sql, array('id'=>$this->getId(), 'idPath'=>$idPath))->execute();
    }
    
    protected function afterUpdate(){
    	if($this->propertyIsChanged('parent_id')){
    		$parent = new self();
            $oldIdPath = $this->getIdPath();
    		if($this->issetData('parent_id') && $this->getParentId() != 0){
    			$parent->load($this->issetData('parent_id'));
    			$idPath = $parent->load($this->getParentId())->getIdPath().$this->getId().'_';
    		}else{
    			$idPath = $this->getId().'_';
    		}
	    	$sql = 'UPDATE `'.$this->getTableName().'` SET'
	    		 . ' `id_path`=concat(:newIdPath:s, substring(`id_path`,'.(strlen($oldIdPath) + 1).'))'
	    		 . ' WHERE `id_path` LIKE :oldIdPath:s';
	    	$this->getDb()->setSql($sql, array('newIdPath'=>$idPath, 'oldIdPath'=>$this->getIdPath().'%'))->execute();
    	}
    }

    protected function initializeProperties() {
        $this->createProperty('id', 'integer', TRUE, TRUE);
        $this->createProperty('category_id', 'integer')
                ->setInputType('hidden')
                ->setRequired(TRUE);
        $this->createProperty('name', 'string')
                ->setInputType('textbox')
                ->setRequired(TRUE)
                ->setLabel('system.label.name')
                ->setVersionable(TRUE);        
        $this->createProperty('parent_id', 'integer')
                ->setInputType('treeselect')
                ->setDefaultValue(0)
                ->setLabel('menu.parent_menu');
        $this->createProperty('url', 'string')
                ->setInputType(NULL)
                ->setRequired(TRUE)
                ->setVersionable(TRUE)
                ->setLabel('menu.url');   
        $this->createProperty('id_path', 'string')
                ->setInputType(NULL)
                ->setRequired(TRUE);         
        $this->createProperty('argument', 'serialize')
                ->setVersionable(TRUE)
                ->setInputType(NULL);
        $this->createProperty('created_time', 'timestamp')
                ->setInputType(NULL)
                ->setInsertable(FALSE)
                ->setUpdateable(FALSE)
                ->setVersionable(TRUE);
        $this->createProperty('role_codes', 'array')
                ->setInputType('select')
                ->setLabel('menu.roles');
        $this->createProperty('status', 'string')
                ->setInputType(NULL)
                ->setRequired(TRUE)
                ->setDefaultValue(CONSTANT_STATUS_ENABLED)
                ->setLabel('system.label.status')
                ->setVersionable(TRUE);   
        $this->createProperty('website_id', 'integer')
                ->setInputType(NULL)
                ->setRequired(TRUE)
                ->setVersionable(TRUE);
        $this->createProperty('sort', 'integer')
                ->setInputType(NULL)
                ->setRequired(TRUE);       
    }

}